1   /*
2    * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 6359948
27   * @summary Check that MXBean operations have the expected ReturnType in MBeanOperationInfo
28   * @author Luis-Miguel Alventosa
29   * @run clean MBeanOperationInfoTest
30   * @run build MBeanOperationInfoTest
31   * @run main MBeanOperationInfoTest
32   */
33  
34  import java.lang.management.*;
35  import javax.management.*;
36  import javax.management.openmbean.*;
37  
38  public class MBeanOperationInfoTest {
39      private static final String[][] returnTypes = {
40          { "dumpAllThreads(boolean,boolean)", "[Ljavax.management.openmbean.CompositeData;" },
41          { "findDeadlockedThreads()", "[J" },
42          { "findMonitorDeadlockedThreads()", "[J" },
43          { "getThreadInfo(long)","javax.management.openmbean.CompositeData"},
44          { "getThreadInfo(long,int)","javax.management.openmbean.CompositeData"},
45          { "getThreadInfo([J)","[Ljavax.management.openmbean.CompositeData;"},
46          { "getThreadInfo([J,int)","[Ljavax.management.openmbean.CompositeData;"},
47          { "getThreadInfo([J,boolean,boolean)","[Ljavax.management.openmbean.CompositeData;"},
48          { "getThreadCpuTime(long)","long"},
49          { "getThreadUserTime(long)","long"},
50          { "resetPeakThreadCount()","void"},
51      };
52      public static void main(String[] args) throws Exception {
53          int error = 0;
54          int tested = 0;
55          MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
56          ObjectName on = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
57          MBeanInfo mbi = mbs.getMBeanInfo(on);
58          MBeanOperationInfo[] ops = mbi.getOperations();
59          for (MBeanOperationInfo op : ops) {
60              String name = op.getName();
61              String rt = op.getReturnType();
62              StringBuilder sb = new StringBuilder();
63              sb.append(name + "(");
64              for (MBeanParameterInfo param : op.getSignature()) {
65                  sb.append(param.getType() + ",");
66              }
67              int comma = sb.lastIndexOf(",");
68              if (comma == -1) {
69                  sb.append(")");
70              } else {
71                  sb.replace(comma, comma + 1, ")");
72              }
73              System.out.println("\nNAME = " + sb.toString() + "\nRETURN TYPE = " + rt);
74              for (String[] rts : returnTypes) {
75                  if (sb.toString().equals(rts[0])) {
76                      if (rt.equals(rts[1])) {
77                          System.out.println("OK");
78                          tested++;
79                      } else {
80                          System.out.println("KO: EXPECTED RETURN TYPE = " + rts[1]);
81                          error++;
82                      }
83                  }
84              }
85          }
86          if (error > 0) {
87              System.out.println("\nTEST FAILED");
88              throw new Exception("TEST FAILED: " + error + " wrong return types");
89          } else if (tested != returnTypes.length &&
90                     !System.getProperty("java.specification.version").equals("1.5")) {
91              System.out.println("\nTEST FAILED");
92              throw new Exception("TEST FAILED: " + tested + " cases tested, " +
93              returnTypes.length + " expected");
94          } else {
95              System.out.println("\nTEST PASSED");
96          }
97      }
98  }